// WorkReportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WorkScheduler.h"
#include "WorkReportDlg.h"


// CWorkReportDlg dialog

IMPLEMENT_DYNAMIC(CWorkReportDlg, CDialog)

CWorkReportDlg::CWorkReportDlg(CWnd* pParent, const std::list<WorkCycle> &lstWorkCycles)
	: CDialog(CWorkReportDlg::IDD, pParent)
	, m_lstWorkCycles(lstWorkCycles)
{
}

CWorkReportDlg::~CWorkReportDlg()
{
}

void CWorkReportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_WORK_CYCLES, m_lstCtrlWorkCycles);
}

BOOL CWorkReportDlg::OnInitDialog()
{
	__super::OnInitDialog();
	InitReportListCtrl();
	FillReportListCtrl();

	return TRUE;
}


BEGIN_MESSAGE_MAP(CWorkReportDlg, CDialog)
END_MESSAGE_MAP()


// CWorkReportDlg message handlers

void CWorkReportDlg::InitReportListCtrl()
{	
	CRect rcLstWorkCycles;
	m_lstCtrlWorkCycles.GetWindowRect(rcLstWorkCycles);
	//leave 20 px for the vscrollbar:
	int columnWidth = (rcLstWorkCycles.Width() -20)/2;
	m_lstCtrlWorkCycles.InsertColumn(1, _T("Start time"), 0, columnWidth);
	m_lstCtrlWorkCycles.InsertColumn(2, _T("End time"), 0, columnWidth);
}

void CWorkReportDlg::FillReportListCtrl()
{
	for (std::list<WorkCycle>::iterator it = m_lstWorkCycles.begin(); it != m_lstWorkCycles.end(); it++)
	{
		WorkCycle &workCycle = *it;
		LVITEM workItem;
		workItem.iItem = m_lstCtrlWorkCycles.GetItemCount();
		
		CString csStartTime = workCycle.m_startTime.Format(_T("%X, %x"));
		CString csEndTime = (workCycle.IsFinished() ? workCycle.m_endTime.Format(_T("%X, %x")) : _T(" - "));
		int idx = m_lstCtrlWorkCycles.InsertItem(m_lstCtrlWorkCycles.GetItemCount(), csStartTime);
		m_lstCtrlWorkCycles.SetItemText(idx, 1, csEndTime);
	}
}